package com.zm.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.sun.corba.se.impl.orb.ParserTable.TestAcceptor1;
import com.zm.bean.TestUserBean;
import com.zm.dao.TestUserBeanMapper;
import com.zm.utils.Myutils;

/**
 * @author Ming
 *
 */
@Controller
public class LoginAction {
	@Resource
	private TestUserBeanMapper test;
	@Resource
	private Myutils Myutils;
	
	private TestUserBean saveUser;

	@RequestMapping("/queryLogin")
	public String loginQuery(TestUserBean user) {
		List<TestUserBean> user1 = test.queryLogin(user);
		// 验证登录
		if (user1 != null && user1.size() > 0) {
			return "../logined/main";
		} else {
			return "../../Login";
		}
	}
	@RequestMapping("/find")
	public String loginQuery2(TestUserBean user) {
		List<TestUserBean> user1 = test.queryLogin(user);
		// 验证登录
		if (user1 != null && user1.size() > 0) {
			return "../logined/main";
		} else {
			return "Login";
		}
	}

	@RequestMapping("/saveUser")
	public String userAdd(HttpServletRequest request, TestUserBean user,
			@RequestParam("file_userurl") MultipartFile part) {
		System.out.println("文件上传前：" + user);
		// 单个文件上传的方法(request,保存数据的实体bean，当个文件的MulitpartFile对象文件的前缀，文件的保存路径)
		// user=Myutils.uploadFils(request, user, part, "user", "buddha");
		// 多个文件上传的方法(request,保存数据的实体bean，文件的前缀，文件的保存路径)
		user = Myutils.uploadFils2(request, user, "user", "buddha");
		// 将用户上传至数据库
		int i = test.insertSelective(user);
		System.out.println("文件上传后：" + user);
		// 验证登录
		if (i > 0) {
			return "../logined/userAdd";
		} else {
			return "index";
		}
	}

	@ResponseBody
	@RequestMapping("/queryUser")
	public List<TestUserBean> queryUser(TestUserBean user) {
		//将查询条件注入bean属性当中
		saveUser=user;
		List<TestUserBean> user1 = test.queryUserInfo(user);
		return user1;
	}
	//queryPathVariable
	@ResponseBody
	@RequestMapping("/queryPathVariable/{userid}")
	public  List<TestUserBean> queryPathVariable(@PathVariable("userid")Integer userid){
		//查询数据为userid={userid}的数据
		List<TestUserBean> listPath=test.queryByUserid(userid);
		return listPath;
	}
	/**
	 * 查询所有成员数据
	 * 使用GET请求方式
	 * 使用ResponseEntity返回json数据
	 * @return
	 */
	@RequestMapping(value="/queryResponseEntity/",method=RequestMethod.GET)
	public ResponseEntity<List<TestUserBean>>queryResponseEntity(){
		List<TestUserBean> list=test.queryUserInfoAll();
		//第一种ResponseEntity返回json方式，静态方法返回
		return ResponseEntity.status(HttpStatus.OK).body(list);
	}
	@ResponseBody
	@RequestMapping("/queryResponseEntity2")
	public JSONPObject queryResponseEntity2(String callbackparam){
		List<TestUserBean> list=test.queryUserInfoAll();
		//第一种ResponseEntity返回json方式，静态方法返回
		return new JSONPObject(callbackparam,list);
	}
	/**
	 * 查询单个成员
	 * 使用GET请求方式
	 * 使用ResponseEntity返回json数据
	 * @return
	 */
	@RequestMapping(value="/queryResponseEntity/{userid}",method=RequestMethod.GET)
	public ResponseEntity<List<TestUserBean>>queryResponseEntity(@PathVariable("userid")Integer userid){
		List<TestUserBean> list=test.queryByUserid(userid);
		//第二种ResponseEntity返回json方式，匿名对象返回，使用构造参数返回
		return new  ResponseEntity<List<TestUserBean>>(list,HttpStatus.OK);
	}
	/**
	 * 逻辑删除行，并返回删除后的结果行
	 * @param userid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteBy1/{userid}", method=RequestMethod.DELETE)
	public List<TestUserBean> deleteUser(@PathVariable("userid")Integer userid) {
		//返回逻辑删除修改行
		int i=test.deleteBy1(userid);
		//删除后查询行
		List<TestUserBean>list=test.queryUserInfo(saveUser);
		return list;
	}
	@RequestMapping("/saveByidBean/{userid}")
	public ResponseEntity<Void> saveByidBean(HttpServletRequest request,@PathVariable("userid")Integer userid,TestUserBean user){
		System.out.println("文件上传前：" + user);
		// 单个文件上传的方法(request,保存数据的实体bean，当个文件的MulitpartFile对象文件的前缀，文件的保存路径)
		// user=Myutils.uploadFils(request, user, part, "user", "buddha");
		// 多个文件上传的方法(request,保存数据的实体bean，文件的前缀，文件的保存路径)
		user = Myutils.uploadFils2(request, user, "user", "buddha");
		// 将用户上传至数据库
		int i = test.updateByPrimaryKey(user);
		System.out.println("文件上传后：" + user);
		return new ResponseEntity<>(HttpStatus.OK);
	}

}
